package com.pro.service.imple;

import com.pro.mapper.JsCourseJournalMapper;
import com.pro.mapper.JsOperationLogMapper;
import com.pro.mapper.JsProductMapper;
import com.pro.pojo.*;
import com.pro.service.JsProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

@Service
public class JsProductServiceImple implements JsProductService {
    @Autowired(required = false)
    private JsProductMapper mapper;

    @Autowired(required = false)
    private JsOperationLogMapper jsOperationLogMapper;
    @Autowired(required = false)
    private JsCourseJournalMapper jsCourseJournalMapper;
    @Override
    public JsonData getTable(JsProduct jsProduct) {
        List<JsProduct> table = mapper.getTable(jsProduct);
        int count = count(jsProduct);
        return table!=null?JsonData.buildSuccess(table,count):JsonData.buildError("服务端错误");
    }

    @Override
    public JsonData getProduct(Integer productId,Integer updateId) {
        //添加操作日志
        JsOperationLog jsOperationLog = new JsOperationLog();
        jsOperationLog.setUpdateId(updateId);
        jsOperationLog.setOperationType(3);
        jsOperationLog.setExecutionTime(new Date());
        jsOperationLog.setOperationList("商品查看列表");
        jsOperationLog.setType("1");
        jsOperationLogMapper.addOperationLog(jsOperationLog);
        JsProduct product = mapper.getProduct(productId);
        Integer saleCount = getSaleCount(productId);
        Integer collectionCount = getCollectionCount(productId);
        product.setSaleNum(saleCount);
        product.setCollectionNum(collectionCount);
        return product!=null?JsonData.buildSuccess(product):JsonData.buildError("服务端错误");
    }

    @Override
    public Boolean addProduct(JsProduct jsProduct) {
        Boolean aBoolean = mapper.addProduct(jsProduct);
        JsOperationLog jsOperationLog = new JsOperationLog();
        jsOperationLog.setUpdateId(jsProduct.getCategroyId());
        jsOperationLog.setOperationType(1);
        jsOperationLog.setExecutionTime(new Date());
        jsOperationLog.setOperationList("添加商品列表");
        jsOperationLog.setType("1");
        jsOperationLogMapper.addOperationLog(jsOperationLog);
        return aBoolean;
    }

    @Override
    public JsonData updateProduct(JsProduct jsProduct,Integer updateId) {
        Boolean aBoolean = mapper.updateProduct(jsProduct);

        JsOperationLog jsOperationLog = new JsOperationLog();
        jsOperationLog.setUpdateId(updateId);
        jsOperationLog.setOperationType(2);
        jsOperationLog.setExecutionTime(new Date());
        jsOperationLog.setOperationList("修改商品列表");
        jsOperationLog.setType("1");
        jsOperationLogMapper.addOperationLog(jsOperationLog);
        //查看商品信息
        JsProduct product = mapper.getProduct(jsProduct.getProductId());
        //判断价格是否变化,进行历史价格日志的添加
        if (product.getOriginalpriceProduct() != jsProduct.getCurrentpriceProduct()){
            JsCourseJournal jsCourseJournal = new JsCourseJournal();
            jsCourseJournal.setCourseId(jsProduct.getProductId());
            jsCourseJournal.setCourseName(jsProduct.getProductName());
            jsCourseJournal.setCourseStatus(jsProduct.getProductSate().toString());
            jsCourseJournal.setUserId(updateId);
            jsCourseJournal.setUpdateDate(new Date());
            jsCourseJournal.setCoursePrice(product.getOriginalpriceProduct());
            jsCourseJournal.setCourseNprice(jsProduct.getCurrentpriceProduct());
            jsCourseJournalMapper.addJournal(jsCourseJournal);
        }
        return aBoolean!=false?JsonData.buildSuccess(aBoolean):JsonData.buildError("服务端错误");
    }

    @Override
    public JsonData delProduct(Integer productId,Integer updateId) {
        Boolean aBoolean = mapper.delProduct(productId);
        JsProduct product = mapper.getProduct(productId);
        JsOperationLog jsOperationLog = new JsOperationLog();
        jsOperationLog.setUpdateId(updateId);
        jsOperationLog.setOperationType(4);
        jsOperationLog.setExecutionTime(new Date());
        jsOperationLog.setOperationList("删除商品列表");
        jsOperationLog.setType("1");
        jsOperationLogMapper.addOperationLog(jsOperationLog);
        return aBoolean!=false?JsonData.buildSuccess(aBoolean):JsonData.buildError("服务端错误");
    }
    /**
     *
     * @author ZPT
     * @date 2021/11/8 10:22
     * @content : 个数
     */
    @Override
    public int count(JsProduct jsProduct) {
        return mapper.count(jsProduct);
    }

    @Override
    public Boolean updateProductstatus(String productSate, Integer productId) {
        return mapper.updateProductstatus(productSate,productId);
    }
    @Override
    public JsonData getSearchProduct(JsProduct jsProduct) {
        List<JsProduct> list = mapper.getSearchProduct(jsProduct);
        int count = count(jsProduct);
        return list!=null?JsonData.buildSuccess(list,count):JsonData.buildError("服务端错误");
    }

    @Override
    public Integer getSaleCount(Integer productCode) {
        return mapper.getSaleCount(productCode);
    }

    @Override
    public Integer getCollectionCount(Integer productId) {
        return mapper.getCollectionCount(productId);
    }

    @Override
    public int productSate() {
        return mapper.productSate();
    }
}
